package exampleQuestions;

public class FIndElementinSortedMAtrix {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int [][]matrix = {{1, 3, 5, 9, 18}, {2, 10, 15, 23, 24}, {7, 11, 20, 36, 38}, {12, 13, 33, 38, 41}};
		
		System.out.println(findElement(20, matrix, 4, 5));
	}

	private static boolean findElement(int el, int[][] matrix, int n, int m) {
		
		int col = m-1;
		int row = 0;
		while (col >= 0 && row < n) {
			if (matrix[row][col] == el) {
				return true;
			}
			if (matrix[row][col] > el) {
				col--;
			} else {
				row++;
			}
		}
		
		return false;
	}

}
